﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    public class MPTagBll
    {
        /// <summary>
        /// 标签列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<MPTagsModel>> GetTageList()
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                var tagsList = new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).GetTagsList();
                return tagsList;
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建标签
        /// </summary>
        /// <param name="tagName">标签名</param>
        public async Task CreateTag(string tagName)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).CreateTag(tagName);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 编辑标签
        /// </summary>
        /// <param name="tagsModel">标签模型</param>
        public async Task UpdateTag(MPTagsModel tagsModel)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).UpdateTag(tagsModel.id, tagsModel.name);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id">标签Id</param>
        public async Task DeleteTag(int id)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).DeleteTag(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 批量为用户打标签
        /// </summary>
        /// <param name="userModel">用户模型类</param>
        /// <param name="tagId">标签Id</param>
        public async Task BatchTagged(MPUserModel userModel, string tagId)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                List<string> openIdList = new List<string>(userModel.openid.Split(','));
                new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).BatchTagged(tagId.ToInt(), openIdList);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 批量为用户取消标签
        /// </summary>
        /// <param name="userModel">用户模型类</param>
        /// <param name="tagId">标签Id</param>
        public async Task BatchUnTagged(MPUserModel userModel, string tagId)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                List<string> openIdList = new List<string>(userModel.openid.Split(','));
                new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).BatchUnTagged(tagId.ToInt(), openIdList);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
    }
}
